package com.gusha.controller;

import java.math.BigInteger;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gusha.config.SysOperateLog;
import com.gusha.dao.InfoContentDao;
import com.gusha.dao.SysCommentDao;
import com.gusha.dao.UserDao;
import com.gusha.model.InfoContent;
import com.gusha.model.InfoQuestion;
import com.gusha.model.SysComment;
import com.gusha.model.User;
import com.gusha.service.SysCommentService;
import com.gusha.utils.Constants;
import com.gusha.utils.JsonResult;
import com.gusha.utils.WebValueUtil;

@Controller
@RequestMapping("/gx/sysComment")
public class SysCommentController {
	
	@Autowired
	private SysCommentDao sysCommentDao;
	
	@Autowired
	private SysCommentService sysCommentService;
	
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private InfoContentDao infoContentDao;
	
	 @RequestMapping("list")
	    @SysOperateLog(operate = "查看评论列表")
	    public String list(Model model, HttpServletRequest request) {

	        //请求参数
	        String pageIndex = WebValueUtil.getWebValue(request, "pageIndex");
	        String userId = WebValueUtil.getWebValue(request, "userId");
	        String contentsId = WebValueUtil.getWebValue(request, "contentId");
	        //参数封装
	        int pageNo = 0;
	        if (StringUtils.isNotBlank(pageIndex)) {
	            pageNo = Integer.parseInt(pageIndex);
	        }
	        SysComment sysComment = new SysComment();
	        if (StringUtils.isNotBlank(userId)) {
	        	sysComment.setUserId(userDao.getOne(new BigInteger(userId)));
	        }
	        if (StringUtils.isNotBlank(contentsId)) {
	        	sysComment.setContentId(infoContentDao.getOne(new BigInteger(contentsId)));
	        }


	        //获取分页数据
	        Sort sort = new Sort(Sort.Direction.DESC, "commentTime");
	        Pageable pageable = PageRequest.of(pageNo, Constants.DEFAULT_PAGESIZE, sort);
	        Page<SysComment> page = sysCommentService.findAll(pageable, sysComment);
	        
	        List<User> listUser = userDao.findAll();
	        List<InfoContent> listContent= infoContentDao.findAll();
	        model.addAttribute("page", page);
	        model.addAttribute("userId", userId);
	        model.addAttribute("contentId", contentsId);
	        model.addAttribute("listUser", listUser);
	        model.addAttribute("listContent", listContent);
	        return "/admin/sysComment/list";
	    }
	 	@RequestMapping("delete")
	    @ResponseBody
	    @SysOperateLog(operate = "删除XX信息")
	    public JsonResult delete(BigInteger id) {
	        JsonResult result = new JsonResult(false,"删除失败");
	        if (sysCommentDao.existsById(id)) {
	        	sysCommentDao.deleteById(id);
	            result.setFlag(true);
	            result.setMsg("删除成功");
	        }else{
	            result.setFlag(false);
	            result.setMsg("该对象信息不存在");
	            return result;
	        }
	        return result;
	    }

	
}
